unit uNumberFunctionFactory;

interface

uses Contnrs, uNumberFunction;

type

  TNumberFunctionFactory = class
  private
    FRegisteredClasses: TClassList;
    class var Instance: TNumberFunctionFactory;

    constructor CreateInstance;
    destructor  DestroyInstance;
  public
    //dummy constructor and destructor, which shouldn't be called
    constructor Create; deprecated;
    destructor  Destroy; override; deprecated;

    class function GetInstance: TNumberFunctionFactory;

    procedure RegisterFunctionClass(AFunctionClass: TNumberFunctionClass);
    procedure IncreaseFunctionCapacity(AIncrease: Integer);
    function  CreateFunction(const AFunctionName: string): TNumberFunction;
  end;

implementation

{ TNumberFunctionFactory }

constructor TNumberFunctionFactory.Create;
begin

end;

constructor TNumberFunctionFactory.CreateInstance;
begin
  FRegisteredClasses := TClassList.Create;
end;

function TNumberFunctionFactory.CreateFunction(const AFunctionName: string): TNumberFunction;
var
  i: Integer;
  functionClass: TNumberFunctionClass;
begin
  functionClass := nil;

  for i := 0 to FRegisteredClasses.Count - 1 do
  begin
    if TNumberFunctionClass(FRegisteredClasses[i]).GetName = AFunctionName then
    begin
      functionClass := TNumberFunctionClass(FRegisteredClasses[i]);
      break;
    end;
  end;

  if functionClass <> nil then
    Result := functionClass.Create
  else
    Result := nil;
end;

destructor TNumberFunctionFactory.Destroy;
begin

end;

destructor TNumberFunctionFactory.DestroyInstance;
begin
  FRegisteredClasses.Destroy;
end;

class function TNumberFunctionFactory.GetInstance: TNumberFunctionFactory;
begin
  Result := Instance;
end;

procedure TNumberFunctionFactory.RegisterFunctionClass(AFunctionClass: TNumberFunctionClass);
begin
  FRegisteredClasses.Add(AFunctionClass);
end;

procedure TNumberFunctionFactory.IncreaseFunctionCapacity(AIncrease: Integer);
begin
  FRegisteredClasses.Capacity := FRegisteredClasses.Count + AIncrease;
end;

initialization

  TNumberFunctionFactory.Instance := TNumberFunctionFactory.CreateInstance;

finalization

  TNumberFunctionFactory.Instance.DestroyInstance;

end.
